package com.xxl.job.executor.protocol;


import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * @program: springboot-websocket-monikehuduan
 * @description: 22
 * @author: XianJin.Lu
 * @create: 2021-09-06 16:47
 **/
public class MjWebsocketClient {
    //有序集合
    private Map<String,Object> map=new LinkedHashMap<>();
    private String requestBody;
    public MjWebsocketClient(){}
    public MjWebsocketClient(String param){
        this.requestBody=param;
    }

    private static Logger logger = Logger.getLogger(MjWebsocketClient.class);
    public static WebSocketClient client;

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    //重载方法
    public  void find(String s) {

        if(client!=null){
            client.send(s);
        }

        try {
            client = new WebSocketClient(new URI("ws://10.0.7.21:9008"),new Draft_6455()) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                    logger.info("连接成功");
                    //连接成功发送消息
                    client.send(s);
                }
                //这个方法不会阻塞当前线程,当前线程不会等他执行完在执行
                @Override
                public void onMessage(String message) {

                    logger.info("收到消息=========="+message);
                    JSONObject jsonObject = JSON.parseObject(message);
                    Object resultData = jsonObject.get("resultData");
                    if(resultData!=null) {
                        String s1 = JSON.toJSONString(resultData);
                        JSONObject jsonObject1 = JSON.parseObject(s1);
                        String devId = jsonObject1.get("devId").toString();
                        String openState = jsonObject1.get("openState").toString();
                        //单个设备的 状态存到 map集合中
                        map.put(devId, jsonObject1.get("openState"));
                    }

                    if(message.equals("over")){
                        client.close();
                    }

                }

                @Override
                public void onClose(int i, String s, boolean b) {
                    logger.info("链接已关闭");
                }

                @Override
                public void onError(Exception e){
                    e.printStackTrace();
                    logger.info("发生错误已关闭");
                }
            };
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        client.connect();

        logger.info(client.getDraft());
        int i=0;
        while(!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
            //  logger.info("正在连接...");
            i++;
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //设置连接超时断开  要不然会一直阻塞在此
            if(i>8){
                System.out.println("连接超时,请联系管理员");
                break;
            }
        }
    }


    public  void find() {
        try {
            //测试用  ws://113.116.35.114:55551        现场    ws://10.0.7.21:9008
            //ws://localhost:8081/websocket/10
            client = new WebSocketClient(new URI("ws://10.0.7.21:9008"),new Draft_6455()) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                  logger.info("连接成功");

                  //连接成功发送消息
                    client.send(requestBody);
                }
                //这个方法不会阻塞当前线程,当前线程不会等他执行完在执行
                @Override
                public void onMessage(String message) {

               logger.info("收到消息=========="+message);


               // Teacher teacher = JSON.parseObject(message, Teacher.class);
               // System.out.println("转换为teacher实体类==========" + teacher.toString());

           //     response00 = JSON.parseObject(message, Menjing.class);
            //    System.out.println(response00.toString());

                    if(message.equals("over")){
                        client.close();
                    }

                }

                @Override
                public void onClose(int i, String s, boolean b) {
                   logger.info("链接已关闭");
                }

                @Override
                public void onError(Exception e){
                    e.printStackTrace();
                 logger.info("发生错误已关闭");
                }
            };
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        client.connect();

        logger.info(client.getDraft());
        int i=0;
        while(!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
      //  logger.info("正在连接...");
            i++;
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //设置连接超时断开  要不然会一直阻塞在此
            if(i>8){
                System.out.println("连接超时,请联系管理员");
                break;
            }
        }
    }

}

